(function() {
    'use strict';

    /**
     * @ngdoc function
     * @name webApp.controller:ReturnformrCtrl
     * @description
     * # ReturnformrCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('ReturnformrCtrl', ReturnformrCtrl);

    ReturnformrCtrl.$inject = ['$scope', 'dataService', '$location', '$state', 'helper'];

    function ReturnformrCtrl($scope, dataService, $location, $state, helper) {
        //显示
        $scope.listAction = function() {
                // $scope.ordersTotal = 0;
                $scope.pageSize = 10;
                $scope.getreturnformList(1);
            }
            //获取列表
        $scope.getreturnformList = function(pageNo) {
            var query = { page_no: pageNo ? pageNo : "1", page_size: $scope.pageSize };
            if ($scope.tid) {
                query.query = { "tid": $scope.tid };
            }
             // 创建时间
            if ($scope.start_created && $scope.end_created) {
                query.start_created = $scope.start_created;
                query.end_created = $scope.end_created;
            }
            dataService.returnformGet(query).then(function(list) {
                if (list) {
                    $scope.returnformslist = list.return_forms;
                    $scope.returnformTotal = list.total_count;
                }
            });
        }
        

        //添加数据 初始化
        $scope.addAction = function() {
            $scope.returnform = {};
            $scope.parentCat = {};
            var id = $location.search().id;
            if (id) {
                dataService.returnformOne(id).then(function(list) {
                    if (list) {
                        $scope.returnform = list;
                    }
                });
            } else {
                //初始化值
                $scope.returnform.status = "1"; //可选：已创建、已审核、已作废";
                $scope.returnform.tid = ''; //"原订单编号";
                $scope.returnform.reason_id = 1; //退换货原因ID
                $scope.returnform.shipping_type = "韵达"; //物流方式名称
                $scope.returnform.logistics_no = "W0000" + random(); //运单号
                $scope.returnform.buyer_account = "201012231"; //账户
                $scope.returnform.return_item_fee = 100.00; //退货金额
                $scope.returnform.out_item_fee = 200.00; //出货金额
                $scope.returnform.replenish_fee = 100.00; //客户补款金额
                $scope.returnform.refund_fee = 50.00; //商家退款金额
                $scope.returnform.other_fee = 10.00; //邮资费用等其他费用
                $scope.returnform.total_fee = 200.00; //合计金额
                $scope.returnform.return_point = 200.00; //返还积分
                $scope.returnform.buyer_nick = "高丽棒子"; //买家名称
                $scope.returnform.receiver_state = "北京"; //收货人的所在省份
                $scope.returnform.receiver_city = "北京市"; //收货人的所在城市
                $scope.returnform.receiver_district = "石景山区"; //收货人的所在地区
                $scope.returnform.receiver_address = "万达广场110座"; //收货人的详细地址
                $scope.returnform.receiver_mobile = "18111811181"; //收货人的手机号码
                $scope.returnform.receiver_name = "收货人"; //收货人的姓名
                $scope.returnform.receiver_phone = "13858121381"; //收货人的电话号码
                $scope.returnform.receiver_zip = "111111"; //收货人的邮编
                $scope.returnform.remark = "这是备注"; //备注
                // $scope.returnform.in_items = [returnform_items()]; //退入商品明细
                // $scope.returnform.out_items = [returnform_items()]; //换出商品明细
                console.log("11111111", $scope.returnform);
            }

        }

        // 退入商品明细
        function returnform_items() {
            // var return_item_id = "S00" + random(11111, 99999);
            return {
                return_item_id: "1100" + random(11111, 99999),
                return_form_id: "T00" + random(11111, 99999),
                item_id: 100,
                sku_id: 1, //条码
                num: 22,
                price: 110,
                discount_fee: 22,
                payment: 2200,
                remark: '',
            };
        }
        $scope.setOrder = function(order) {
            // console.log("112312312", order);
            if (order) {
                $scope.returnform.tid = order.order_id;
                $scope.returnform.in_items = clone(order.items);
                $scope.returnform.out_items = clone(order.items);
            }
        }
        $scope.orderAdd = function() {

            if (!$scope.returnform.return_form_id) {
                dataService.returnformAdd($scope.returnform).then(function(re) {
                    if (re.return_form_id) {
                        helper.myAlert("success", null, null, function() {
                            $state.go("returnform.list");
                        });
                    } else {
                        helper.myAlert("error " + re.Msg);
                    }
                });
            } else {
                dataService.returnformUpdate($scope.returnform).then(function(re) {
                    if (re.return_form_id) {
                        helper.myAlert("success", null, null, function() {
                            $state.go("returnform.list");
                        });
                    } else {
                        helper.myAlert("error " + re.Msg);
                    }
                });
            }
        }

        //显示
        $scope.infoAction = function() {
            var id = $location.search().id;
            if (id) {
                dataService.returnformOne(id).then(function(list) {
                    if (list) {
                        $scope.returnform = list;
                    } else {
                        helper.myAlert("No data found", null, null, function() {
                            $state.go("returnform.list");
                        });
                    }
                });
            } else {
                helper.myAlert("No data found", null, null, function() {
                    $state.go("returnform.list");
                });
            }
        }
        $scope.statusUpdata = function(id, type) {
            if (id) {
                var data = {return_form_id:id,event:"R_EVENT_REVIEW_REQ"};
                dataService.returnformStatusUpdate(data).then(function(list) {
                    if (list.return_form_id) {
                        helper.myAlert("success");
                    } else {
                        helper.myAlert("No data found");
                    }
                });
            }
        }
    }
})();
